home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 354 < prev    next >
Internet Message Format  |  1996-08-06  |  2KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c.moderated,comp.lang.c,comp.std.c
  4. Subject: Re: HELP IN WRITING MY FIRST PROGRAM ASSINGMENT
  5. Date: 22 Feb 1996 06:41:58 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Approved: seebs
  8. Message-ID: <4ghoam$dp4@solutions.solon.com>
  9. References: <3127FF7A.6442C3B8@eden.com> <4gfhkj$3p8@solutions.solon.com> <4ggbi9$83k@solutions.solon.com> <4ghnnc$dj9@solutions.solon.com>
  10. NNTP-Posting-Host: solutions.solon.com
  11.  
  12. In article <4ghnnc$dj9@solutions.solon.com>,
  13. Keith Edward O'hara <k-ohara@ux7.cso.uiuc.edu> wrote:
  14. >Previously in comp.lang.c.moderated,
  15. >>int
  16. >>cmp(void *a, void *b) {
  17. >>        if (a < b) return -1;
  18. >>                if (a > b) return 1;
  19. >>        else return 0;
  20. >>}
  21. >was used as the callback function for qsort() (in an evil piece of code).
  22.  
  23. >So I had to try it.  The qsort in Borland's library, and the one I wrote
  24. >when I was learning C, both call the callback compare function with one
  25. >address OUTSIDE the original array.  In my qsort, I put the partitioning
  26. >element in temporary storage (which I need anyway to do the swaps) and
  27. >pass the address of the temporary storage as one argument to cmp().
  28.  
  29. Agh!
  30.  
  31. >Two questions follow:
  32. >Did Peter Seebach know all along that the pointers passed to cmp() would be
  33. >not in the same array on some systems? 
  34. >[No.  -mod]
  35.  
  36. To clarify: I believe it is incorrect for the library to do so; although
  37. it's pretty tenuous, the "which is called with two arguments that point to
  38. the objects being compared" seems to mean that to me.  My rationale is that
  39. the compiler *cannot* prove that the objects do not depend on being parts
  40. of a larger array.  For instance, consider an array of 200 objects; sort
  41. the first 100, with a comparison routine that compares not only the objects,
  42. but the objects 100 further in the "real" array.  I believe this to be
  43. conforming.
  44.  
  45. >Does the standard (which I don't have to own since I'm not posting to
  46. >comp.std.c) disallow this behavior of qsort()?
  47. >[I hope so.]
  48.  
  49. Crossposted to comp.std.c.
  50.  
  51. BTW, while we're at it, is it legal for nmemb to be 0 for qsort?  I'd assume
  52. it would just return.
  53.  
  54. -s
  55. -- 
  56. Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
  57. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  58. FUCK the communications decency act.  Goddamned government.  [literally.]
  59. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.txt
  60.